#The DB_Table class provides the data structure and methods
#require to retrieve the table name, column names, and data types.
#
# Public Attributes
# * :tableName - table name
# * :colNames - Array of column names
# * :colDTypes - Array of data types

class DB_Table
  
  attr_reader :tableName, :colNames, :colDTypes
  
  
  #The initialize method
  #instantiates necessary variables
  #and processes the table file
  #into the data structure.
  #Utilizing the no_comments, find_table_name
  #and the find_cols_types methods.
  
  def initialize( aFile )
    
    @file = aFile
    @tableName = ""
    @colNames = Array.new()
    @colDTypes = Array.new()
    @words = Array.new
    @lines = ""
    
    begin
      no_comments
    rescue
      raise
    end
    
    begin
      find_table_name
    rescue
      raise
    end
    
    begin
      find_cols_types
    rescue
      raise
    end
    
  end
  
  #strips inline SQL comments
  def no_inline_comments( s )
    if s.include? "/*" and s.include? "*/"  then
       sC = s.index( '/*' )
       eC = s.index( '*/' )
       c = s.slice( 0, s.index( '/*' ) )
       d = s.slice( ( s.index( '*/' ) + '*/'.length ), s.length )
       s = c + d
      return s
    else return s
    end #if
  end #noInlineComments

  #strips SQL comments lines and comment blocks from table file
  def no_comments
    @line = ""
    cO = false
    
    while @line = @file.gets
      @words = @line.upcase().split(' ')    
      if @words.first =~ /\/\*/                  \
      and @words.last !=~ /\*\//              \
      and @cO == false
      then
        #comment chunk open
        cO = true
      end
      
      if ( @words.first !~ /--/                    \
      and ( @words.first !~ /\/\*/              \
        and @words.last !~ /\*\/Z/  )  )      \
      and cO == false
      then
        @lines += no_inline_comments( @line ).strip
      end
      
      if @words.first !~ /\/\*/                   \
      and @words.last =~ /\*\//               \
      and @cO == true
      then
         #comment chunk closed
         @cO = false
        #print "cO = false"
      end 
    end #while
  end #noComments
  
  #retrieves the table name from a scrubbed SQL table create statement
  def find_table_name
    @strtTblNm = @lines.index( "TABLE" ) \
          + @lines.slice( @lines.index( "TABLE" )..@lines.length ).index( " " ) \
          + 1
    if @lines.slice( @strtTblNm, @lines.length ).index( " " ) \
      < @lines.slice( @strtTblNm, @lines.length ).index( "(" ) \
    then
      @tblNmLngth = @lines.slice( @strtTblNm, @lines.length ).index( " " )
    else
      @tblNmLngth = @lines.slice( @strtTblNm, @lines.length ).index( "(" )
    end
    @tableName = @lines.slice( @strtTblNm, @tblNmLngth )
    @tableName = @tableName.strip
  end #findTableName
  #retrieves the column names and data types
  def find_cols_types
    #getting column names
    curS = @lines[@strtTblNm + @tblNmLngth, @lines.length]
    
    intv = 0
    after1st = false
    lastCol = false
    nxt = "c"
    done = false
    
    while done == false
      if nxt == "c" then      #looking for column?
         if after1st == false then       #is this the first column?
           curS = curS[ curS.index( "(" ) + 1 , curS.length ]
           after1st = true
         else
           curS = curS[ curS.index( "," ) + 1 , curS.length ]
           done == true
         end
         curS = curS.strip
         curS = curS.strip
         intv = curS.index( " " )
         @colNames.push( curS[ 0, intv ] )
         nxt = "t"
         curS = curS[ intv, curS.length ]
      elsif nxt == "t" then  #looking for data  type?
         curS = curS.strip
         intv = curS.index( "," )
         if not intv.nil? then
           @colDTypes.push( curS[ 0, intv ] )
           curS = curS[ intv, curS.length ]
         else
           done = true
           curS = curS[ 0, curS.index( ")" ) ]
           curS = curS.strip
           @colDTypes.push( curS )
         end
         nxt = "c"
      end   
    end #end while
  end #end find_cols_types
end #end class